package top.chukongxiang.monitorservice.javafx.fxml.components;

import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import lombok.Getter;

import java.io.Closeable;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/30 09:34
 */
public class LoadingAlert implements Closeable {

    private final Alert alert;

    @Getter
    private ProgressBar progressBar;

    public LoadingAlert() {
        this("加载中...");
    }

    public LoadingAlert(String contentText) {
        this(null, contentText);
    }

    public LoadingAlert(Window parent, String contentText) {
        alert = new Alert(Alert.AlertType.NONE, contentText);
        if (parent != null) {
            alert.initOwner(parent);
            alert.initModality(Modality.WINDOW_MODAL);
        } else {
            alert.initModality(Modality.APPLICATION_MODAL);
        }
        alert.initStyle(StageStyle.DECORATED);
        alert.setResizable(false);
        alert.getDialogPane().setStyle("-fx-background-color: white;");
    }

    public ProgressBar addProgressBar() {
        progressBar = new ProgressBar();
        progressBar.setPrefWidth(200);
        progressBar.setPrefHeight(30);

        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);

        alert.getDialogPane().setContent(vBox);

        return progressBar;
    }



    public void show() {
        alert.show();
    }


    @Override
    public void close() {
        alert.setResult(ButtonType.CANCEL);
        alert.close();
    }
}
